<?php

class Idcard {

    public $gatewayUrl = "https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
    public $connectTimeout;
    public $appKey;
    public $secretKey;
    public $appCode;

    public function __construct($appKey = "", $secretKey = "", $appCode = "4599e9cc9a204872a41e308238f5393d") {
        $this->appKey = $appkey;
        $this->secretKey = $secretKey;
        $this->appCode = $appCode;
    }

    public function getCardInfo($file, $side = 'face') {
        $image = $this->image_base64(trim($file, '/'));
        $headers = array(
            'Authorization' => "APPCODE ". $this->appCode,
            'Content-Type' => "application/json; charset=UTF-8"
        );
        $data = array(
            'image' => array(
                'dataType' => 50,
                'dataValue' => $image
            ),
            'configure' => array(
                'dataType' => 50,
                'dataValue' => '{"side": "'.$side.'"}'
            )
        );
        $post = "{\"inputs\":[".  json_encode($data)."]}";
        $result = $this->curl($this->gatewayUrl, 'POST', $post, $headers);
        $result = json_decode($result, TRUE);
        if($result){
            $value = $result['outputs'][0]['outputValue']['dataValue'];
            $value = json_decode($value, TRUE);
            return array('success' => true, 'msg' => '请求成功', 'data' => $value);
        }else{
            return array('success' => false, 'msg' => '系统未返回,请尝试缩小图片尺寸或更换图片文件');
        }
        
    }

    public function image_base64($image_file) {
        $image_base64 = '';
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $image_base64 = chunk_split(base64_encode($image_data));
        return $image_base64;
    }

    public function curl($url, $httpMethod = "GET", $postFields = null, $headers = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        //https request
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if (is_array($headers) && 0 < count($headers)) {
            //curl_setopt($ch, CURLOPT_HEADER, true);
            $httpHeaders = self::getHttpHearders($headers);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
        }
        if($httpMethod == "POST"){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            //throw new Exception(curl_error($ch), 0);
            return false;
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                //throw new Exception($response, $httpStatusCode);
                return false;
            }
        }
        curl_close($ch);
        return $response;
    }

    static function getHttpHearders($headers) {
        $httpHeader = array();
        foreach ($headers as $key => $value) {
            array_push($httpHeader, $key . ":" . $value);
        }
        return $httpHeader;
    }

}